package com.bank.dao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bank.pojo.BankNews;
import com.bank.vo.BankNewsResult;

@Component("newsDao")
public class BankNewsDao extends BaseDao{
	@Transactional
	public boolean addNews(BankNews news){
		getSession().save(news);
		return true;
	}
	
	@SuppressWarnings("unchecked")
	@Transactional
	public List<BankNewsResult> getNewsByPage(int pageNum){
		List<BankNewsResult> list=new ArrayList<BankNewsResult>();
		String hql="select new com.bank.vo.BankNewsResult (newsId,newsTitle,newsContent,bankNewsType.newsTypeName,"
					+ "newsDate) from BankNews order by newsId asc";
		list=getSession().createQuery(hql).setMaxResults(5).setFirstResult((pageNum-1)*5).list();
		list.get(0).setNpage(getNewsPage());
		list.get(0).setNsize(getSession().createQuery("from BankNews").list().size());
		return list;
	}
	
	@Transactional
	public int getNewsPage(){
		int size=getSession().createQuery("from BankNews").list().size();
		if(size%5==0){
			return size/5;
		}else{
			return size/5+1;
		}
	}
	
	@Transactional
	public boolean delNews(String id){
		Session session=getSession();
		if(id.contains(",")){
			String[] arr=id.split(",");
			for(String str:arr){
				int nid=Integer.parseInt(str);
				int flag=session.createQuery("delete from BankNews where newsId=?").setInteger(0, nid).executeUpdate();
				if(flag!=1){
					return false;
				}
			}
		}else{
			int flag=session.createQuery("delete from BankNews where newsId=?").setInteger(0,Integer.parseInt(id)).executeUpdate();
			if(flag!=1){
				return false;
			}
		}
		return true;
	}
	
	@SuppressWarnings("unchecked")
	@Transactional
	public List<BankNewsResult> queryNews(String flag,String queryStr,int page){
		List<BankNewsResult> list=new ArrayList<BankNewsResult>();
		int size=0,pageNum=0;
		Session session=getSession();
		String hql="select new com.bank.vo.BankNewsResult (newsId,newsTitle,newsContent,bankNewsType.newsTypeName,"
				+ "newsDate) from BankNews";
		switch(flag){
			case "0":
				hql+=" where newsTitle like ? order by newsId asc";
				list=session.createQuery(hql).setString(0,"%"+queryStr+"%").setMaxResults(5).setFirstResult((page-1)*5).list();
				if(list.size()>0){
					size=session.createQuery(hql).setString(0,"%"+queryStr+"%").list().size();
					pageNum=getQueryResultPage(size);
					list.get(0).setNpage(pageNum);
					list.get(0).setNsize(size);
				}
				break;
			case "1":
				hql+=" where bankNewsType.newsTypeId=? order by newsId asc";
				list=session.createQuery(hql).setInteger(0,Integer.parseInt(queryStr)).setMaxResults(5).setFirstResult((page-1)*5).list();
				if(list.size()>0){
					size=session.createQuery(hql).setString(0,queryStr).list().size();
					pageNum=getQueryResultPage(size);
					list.get(0).setNpage(pageNum);
					list.get(0).setNsize(size);
				}
				break;
			case "2":
				String[] arr=queryStr.split(",");
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				try{
					Date start=sdf.parse(arr[0]);
					Date stop=sdf.parse(arr[1]);
					//System.out.println(sdf.format(start)+","+sdf.format(stop));
					hql+=" where newsDate>=? and newsDate<=? order by newsId asc";
					list=session.createQuery(hql).setDate(0,start).setDate(1,stop).setMaxResults(5).setFirstResult((page-1)*5).list();
					if(list.size()>0){
						size=session.createQuery(hql).setDate(0,start).setDate(1,stop).list().size();
						pageNum=getQueryResultPage(size);
						list.get(0).setNpage(pageNum);
						list.get(0).setNsize(size);
					}
				}catch(Exception e){
					e.printStackTrace();
				}
				break;
		}
		return list;
	}
	
	@Transactional
	public BankNewsResult getNewsById(int nid){
		BankNewsResult news=new BankNewsResult();
		String hql="select new com.bank.vo.BankNewsResult (newsId,newsTitle,newsContent,bankNewsType.newsTypeName,"
				+ "newsDate) from BankNews where newsId=?";
		news=(BankNewsResult)getSession().createQuery(hql).setInteger(0,nid).uniqueResult();
		return news;
	}
	
	public int getQueryResultPage(int size){
		if(size%5==0){
			return size/5;
		}else{
			return size/5+1;
		}
	}
	
	@Transactional
	public boolean updateNews(BankNews news){
		getSession().update(news);
		return true;
	}
}
